﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DropHandle : MonoBehaviour 
{
	RaycastHit hit;

	void Start()
	{
		StartCoroutine (OnMouseDown ());
	}

//	void Update()
//	{
//		if (Input.GetMouseButtonDown (0)) {
//			if (Physics.Raycast (Camera.main.ScreenPointToRay (Input.mousePosition), out hit)) {
//				if (hit.collider.gameObject != gameObject)
//					return;
//
//			}
//		} 
//	}

	IEnumerator OnMouseDown()
	{
		Vector3 screenSpace = Camera.main.WorldToScreenPoint(transform.position);
		Vector3 offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));

		while (Input.GetMouseButton (0)) {
			Vector3 curScreenSpace = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
			Vector3 curPosition = Camera.main.ScreenToWorldPoint (curScreenSpace) + offset;
			transform.position = curPosition;

			yield return new WaitForFixedUpdate ();
		}
	}

	void OnMouseButton()
	{
		Debug.Log ("up");
	}
}
